/*******************************************************************************
 * Qantiqa : Decentralized microblogging platform
 * Copyright (C) 2010 Dario (i@dario.im) 
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 ******************************************************************************/

package controllers;

import java.util.List;

import models.Gluon;
import play.data.validation.Required;
import play.data.validation.Validation;
import play.modules.gae.GAE;
import play.mvc.Before;
import play.mvc.Controller;

/**
 * Gluon CRUD admin tool.
 * 
 * Allows to manage the official gluon list for Qantiqa overlay.
 * 
 * @author Dario
 */
public class Gluons extends Controller {

	/**
	 * Aspect to check if our user is authenticated.
	 */
	@Before
	static void checkConnected() {
		if (GAE.getUser() == null) {
			// Redirect the user to login...
			Higgs.login();
		} else {
			if (!GAE.isAdmin()) {
				// Only allow user admins.
				Higgs.login();
			}

			renderArgs.put("user", GAE.getUser().getEmail());
		}
	}

	/**
	 * Auxiliary method to check if a gluon already exists.
	 * 
	 * @param id
	 *            Gluon id (autogenerated by Siena)
	 * 
	 * @return A gluon
	 */
	static Gluon checkIfExistsGluon(Long id) {
		Gluon g = Gluon.findById(id);
		if (g == null) {
			flash.error("Gluon with id %d not found", id);
			index();
		}

		return g;
	}

	public static void index() {
		List<Gluon> gluons = Gluon.all().fetch();
		render(gluons);
	}

	/**
	 * Renders the main screen to add gluons.
	 */
	public static void blank() {
		render();
	}

	/**
	 * POST method to create gluons from {{@link #blank()}.
	 * 
	 * @param host
	 *            Gluon's WAN IP.
	 * @param port
	 *            Gluon's published port.
	 * @param secret
	 *            Gluon's secret (using the generated one by Play with
	 *            "play secret" and stored in qa-node-api/conf/application.conf.
	 */
	public static void create(@Required String host, @Required Integer port,
			@Required String secret) {
		if (Validation.hasErrors()) {
			flash.error("Some fields are missing");
			blank();
		}

		// endpoint = ip + port where to find a gluon
		Gluon g = Gluon.findByEndpoint(host, port);
		if (g != null) {
			flash.error("Gluon already created");
			index();
		}

		flash.success("Gluon successfully created");
		new Gluon(host, port, play.libs.Codec.hexMD5(secret)).insert();

		index();
	}

	/**
	 * Enable gluon in the official list.
	 * 
	 * @param id
	 *            Gluon's id.
	 */
	public static void enable(Long id) {
		Gluon g = checkIfExistsGluon(id);
		g.active = Boolean.TRUE;
		g.update();

		flash.success("Gluon successfully enabled");

		index();
	}

	/**
	 * Disable gluon in the official list.
	 * 
	 * @param id
	 *            Gluon's id.
	 */
	public static void disable(Long id) {
		Gluon g = checkIfExistsGluon(id);
		g.active = Boolean.FALSE;
		g.update();

		flash.success("Gluon successfully disabled");

		index();
	}

	/**
	 * Delete a gluon.
	 * 
	 * @param id
	 *            Gluon's id.
	 */
	public static void delete(Long id) {
		Gluon g = checkIfExistsGluon(id);
		g.delete();

		flash.success("Gluon successfully deleted");

		index();
	}
}
